package com.niit.activity.service;

import com.niit.activity.entity.Activity;
import com.niit.activity.entity.Topic;
import com.niit.activity.mapper.ActivityMapper;
import com.niit.activity.mapper.LikeMapper;
import com.niit.activity.mapper.TopicMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private TopicService topicService;

    public Activity getActivityById(Long id) {
        return activityMapper.getActivityById(id);
    }
    public Topic getTopicByActivityId(Long activityId) {
        Activity activity = activityMapper.getActivityById(activityId);
        if (activity != null && activity.getTopicId() != null) {
            return topicService.getTopicById(activity.getTopicId());
        }
        return null;
    }

    public String getTopicContentByActivityId(Long activityId) {
        Activity activity = activityMapper.getActivityById(activityId);
        if (activity != null && activity.getTopicId() != null) {
            return topicService.getTopicContentById(activity.getTopicId());
        }
        return null;
    }

    public void insertActivity(Activity activity) {
        if (activity.getUserId() == null || activity.getUserId() <= 0) {
            throw new IllegalArgumentException("用户未登录或用户ID无效，无法发布动态");
        }
        if (activity.getTopicId() != null) {
            topicService.updateTopicNum(activity.getTopicId());
        }
        activityMapper.insertActivity(activity);
    }

    public List<Activity> findPublicActivity() {
        return activityMapper.findPublicActivity();
    }

    public List<Activity> getActivitiesByUserId(Long userId) {
        return activityMapper.selectByUserId(userId);
    }

    public boolean updateActivity(Activity activity) {
        int affectedRows = activityMapper.updateActivity(activity);
        return affectedRows > 0;
    }

    public void deleteActivityById(Long id) {
        activityMapper.markAsDeleted(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean toggleLikeActivity(Long activityId, Long userId) {
        try {
            Integer currentStatus = likeMapper.getLikeStatus(userId, activityId);

            if (currentStatus == null) {
                likeMapper.insertLike(userId, activityId);
                activityMapper.incrementLikeCount(activityId);
                return true;
            } else {
                int newStatus = (currentStatus == 1) ? 0 : 1;
                likeMapper.updateLikeStatus(userId, activityId, newStatus);

                if (newStatus == 1) {
                    activityMapper.incrementLikeCount(activityId);
                } else {
                    activityMapper.decrementLikeCount(activityId);
                }
                return newStatus == 1;
            }
        } catch (Exception e) {
            log.error("点赞操作异常", e);
            throw new RuntimeException("点赞操作失败", e);
        }
    }

    public boolean isUserLikedActivity(Long activityId, Long userId) {
        if (userId == null || userId <= 0) {
            return false;
        }

        Integer status = likeMapper.getLikeStatus(userId, activityId);
        return status != null && status == 1;
    }

    public int getLikeCountByActivityId(Long activityId) {
        return likeMapper.countLikesByActivityId(activityId);
    }

}